/*******************************************************************************
 * Copyright 2011 Mauro Luigi Drago (drago.luigi@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.bytenose.extrautils.logging;

import java.util.ArrayList;
import java.util.List;

import com.bytenose.extrautils.annotations.NonNullable;
import com.google.common.collect.*;

/**
 * An error aware logger forwarding messages to an underlying logger.
 * Disposing this object through the {@link ILogger#dispose()} method
 * only releases this logger specific resources (i.e., the underlying logger
 * is not disposed) unless the {@code disposeNested} flag is set
 * during construction.
 * 
 * @author Mauro Luigi Drago
 *
 * @param ErrorType the type of errors to log.
 */
public final class ForwardingErrorLogger<ErrorType> implements ILogger, IErrorLogger<ErrorType> {
	/** The nested logger */
	private final ILogger nestedLogger;
	
	/** The dispose nested flag */
	private final boolean disposeNested;
	
	/** The list of errors */
	private final List<ErrorType> errors = new ArrayList<ErrorType>();
	
	/**
	 * Creates a new {@link ForwardingErrorLogger} with the {@code disposeNested} flag
	 * set to {@code false}.
	 * 
	 * @param nestedLogger the nested logger, cannot be null.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable arguments are null.
	 */
	public ForwardingErrorLogger(@NonNullable ILogger nestedLogger) 
			throws IllegalArgumentException {
		if (nestedLogger == null) throw new IllegalArgumentException("nestedLogger");
		this.nestedLogger = nestedLogger;
		this.disposeNested = false;
	}
	
	/**
	 * Creates a new {@link ForwardingErrorLogger}.
	 * 
	 * @param nestedLogger the nested logger.
	 * @param disposeNested the nested logger disposition flag.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable arguments are null.
	 */
	public ForwardingErrorLogger(@NonNullable ILogger nestedLogger, boolean disposeNested) 
			throws IllegalArgumentException {
		if (nestedLogger == null) throw new IllegalArgumentException("nestedLogger");
		this.nestedLogger = nestedLogger;
		this.disposeNested = disposeNested;
	}
	
	/**
	 * Creates a new {@link ForwardingErrorLogger} with the {@code disposeNested} flag
	 * set to {@code false}.
	 * 
	 * @param <T> the type of errors.
	 * @param nestedLogger the nested logger, cannot be null.
	 * @return the new logger.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable argements are null.
	 */
	public static <T> ForwardingErrorLogger<T> create(@NonNullable IStreamLogger nestedLogger) 
			throws IllegalArgumentException {
		return new ForwardingErrorLogger<T>(nestedLogger);
	}
	
	/**
	 * Creates a new {@link ForwardingErrorLogger}.
	 * 
	 * @param <T> the type of errors.
	 * @param nestedLogger the nested logger, cannot be null.
	 * @param disposeNested the nested logger disposition flag.
	 * @return the new logger.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable arguements are null.
	 */
	public static <T> ForwardingErrorLogger<T> create(@NonNullable IStreamLogger nestedLogger,
			boolean disposeNested) 
			throws IllegalArgumentException {
		return new ForwardingErrorLogger<T>(nestedLogger, disposeNested);
	}
	
	@Override
	public void logInfo(String message) {
		nestedLogger.logInfo(message);
	}

	@Override
	public void logWarning(String message) {
		nestedLogger.logWarning(message);
	}

	@Override
	public void logError(String message) {
		nestedLogger.logError(message);
	}

	@Override
	public void logError(Throwable e) {
		nestedLogger.logError(e);
	}
	
	@Override
	public void logError(ErrorType error) {
		nestedLogger.logError(error.toString());
	}
	
	@Override
	public void logError(String message, Throwable e) {
		nestedLogger.logError(message, e);
	}

	@Override
	public void addError(ErrorType error) {
		errors.add(error);
	}

	@Override
	public void logAddError(ErrorType error) {
		logError(error);
		errors.add(error);
	}
	
	@Override
	public void logErrors(Iterable<? extends ErrorType> errors) {
		for (ErrorType error : errors) {
			logError(error);
		}
	}

	@Override
	public void addErrors(Iterable<? extends ErrorType> errors) {
		for (ErrorType error : errors) {
			addError(error);
		}
	}

	@Override
	public void logAddErrors(Iterable<? extends ErrorType> errors) {
		for (ErrorType error : errors) {
			logAddError(error);
		}
	}

	@Override
	public Iterable<ErrorType> getErrors() {
		return Iterables.unmodifiableIterable(errors);
	}

	@Override
	public boolean isErroneous() {
		return !errors.isEmpty();
	}

	@Override
	public void dispose() {
		if (disposeNested) nestedLogger.dispose();
	}
}
